package com.jiantong.web.controller;

import com.google.common.collect.Maps;
import com.jiantong.lawEnforcing.dto.CasesBasicDto;
import com.jiantong.lawEnforcing.enums.LawCasesType;
import com.jiantong.lawEnforcing.model.*;
import com.jiantong.lawEnforcing.service.*;
import com.jiantong.user.model.AppUserRole;
import com.jiantong.user.model.User;
import com.jiantong.user.service.AppUserRoleReadService;
import com.jiantong.user.service.UserReadService;
import com.jiantong.web.util.SendMessage;
import com.jiantong.work.enums.HarmlessFormsType;
import com.jiantong.work.model.*;
import com.jiantong.work.service.*;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by niufei on 2017/4/19.
 *
 */
@RestController
@Slf4j
public class TestController {

    private final static SimpleDateFormat DFT = new SimpleDateFormat("yyyy/MM/dd");

    @RpcConsumer
    private HarmlessReadService harmlessReadService;

    @RpcConsumer
    private AnimalCertificateService animalCertificateService;

    @RpcConsumer
    private CreditProductService creditProductService;

    @RpcConsumer
    private QuarantineCenterHarmlessReadService quarantineCenterHarmlessReadService;

    @GetMapping(value = "test/forms/{animalType}")
    public Map<Integer ,List> findAWeekHarmlessForms(@PathVariable(value = "animalType") Integer animalType) throws ParseException {

        Map<Integer ,List> result = Maps.newHashMap();
        Response<List<Map<String , Object>>> isLocalResponse = harmlessReadService.findAWeekSumIsLocal(animalType);
        if(!isLocalResponse.isSuccess()){
            log.error("failed.a.week.sum.isLocal",isLocalResponse.getError());
            throw new OPServerException(isLocalResponse.getError());
        }
        Response<List<Map<String , Object>>> sumReportType = harmlessReadService.findAWeekSumReportType(animalType);
        if(!sumReportType.isSuccess()){
            log.error("failed.a.week.sum.ReportType",sumReportType.getError());
            throw new OPServerException(sumReportType.getError());
        }
        Response<List<Map<String , Object>>> qResponse = quarantineCenterHarmlessReadService.findAWeekSumByProductType(animalType);
        if(!qResponse.isSuccess()){
            log.error("failed.a.weekSum.quarantine",qResponse.getError());
            throw new OPServerException(qResponse.getError());
        }
        List<Map<String , Object>> maps1 = new ArrayList<>();//产地检疫
        List<Map<String , Object>> maps2 = new ArrayList<>();//外市入场
        List<Map<String , Object>> maps3 = new ArrayList<>();//本市入场
        List<Map<String , Object>> maps4 = new ArrayList<>();//宰前检查
        List<Map<String , Object>> maps5 = new ArrayList<>();//同步检疫
        List<Map<String , Object>> maps6 = new ArrayList<>();//防疫中心
        for(Map<String , Object> map :isLocalResponse.getResult()){
            if((int)map.get("isLocal") == -1){
                maps2.add(map);
            }else {
                maps3.add(map);
            }
        }
        for(Map<String , Object> map :sumReportType.getResult()){
            switch ((int)map.get("reportType")){
                case 1:
                    maps1.add(map);
                    break;
                case 5:
                    maps4.add(map);
                    break;
                case 6:
                    maps5.add(map);
                    break;
            }
        }
        maps1 = make(maps1);
        maps2 = make(maps2);
        maps3 = make(maps3);
        maps4 = make(maps4);
        maps5 = make(maps5);
        maps6 = make(qResponse.getResult());
        result.put(HarmlessFormsType.QUARANTINE_ORIGIN_AREA.value(),maps1);
        result.put(HarmlessFormsType.ADMISSION_INSPECTION_NON_LOCAL.value(),maps2);
        result.put(HarmlessFormsType.ADMISSION_INSPECTION_LOCAL.value(),maps3);
        result.put(HarmlessFormsType.ANTEMORTEM_INSPECTION.value(),maps4);
        result.put(HarmlessFormsType.SYNCHRONOUS_INSPECTION.value(),maps5);
        result.put(HarmlessFormsType.QUARANTINE_CENTER_HARMLESS.value(),maps6);

        return result;
    }

    private List<Map<String , Object>> make(List<Map<String , Object>> list) throws ParseException {
        List<Map<String , Object>> mapList = new ArrayList<>();
        Calendar[] calendars = new Calendar[7];
        Map<String , Object> temp = null;

        for (int i = 0 ; i<7;i++){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MILLISECOND,0);
            calendar.set(Calendar.DATE ,calendar.get(Calendar.DATE) - i);
            calendars[i] = calendar;
            temp = Maps.newHashMap();
            temp.put("date",DFT.format(calendar.getTime()));
            temp.put("quantity",0);
            mapList.add(temp);
        }
        if(list == null || list.size() == 0){
            return mapList;
        }else{
            for(Map<String , Object> map : list){
                Calendar calendarTemp = Calendar.getInstance();
                Date date = DFT.parse(map.get("date").toString());
                calendarTemp.setTime(date);
                for(int i = 0 ; i < 7 ; i ++){
                    if(calendarTemp.equals(calendars[i])){
                        Map<String ,Object> mapTemp = Maps.newHashMap();
                        mapTemp.put("date",map.get("date"));
                        mapTemp.put("quantity",map.get("quantity"));
                        mapList.set(i,mapTemp);
                    }
                }
            }
            return mapList;
        }
    }

//--------------------------------------------------------------------------------------------

    @RpcConsumer
    private BreedReportReadService breedReportReadService;
    @RpcConsumer
    private BreedReportSurveyRecordReadService breedReportSurveyRecordReadService;

    @RpcConsumer
    private  ButcherReportSurveyRecordReadService butcherReportSurveyRecordReadService;
    @RpcConsumer
    private ButcherReportReadService butcherReportReadService;

    @RpcConsumer
    private SupervisionRecordsReadService supervisionRecordsReadService;


    @GetMapping(value = "/test/AB")
    public IssuanceTable testAB( Integer animalType) throws ParseException {
        //造出来BreedReport breedReport, BreedReportSurveyRecord breedReportSurveyRecord 当做参数传入
        BreedReport breedReport = breedReportReadService.findById(61L).getResult();
        BreedReportSurveyRecord breedReportSurveyRecord = breedReportSurveyRecordReadService.findByReportId(61L).getResult();

        ButcherReportSurveyRecord butcherReportSurveyRecord = butcherReportSurveyRecordReadService.findById(48L).getResult();
        ButcherReport butcherReport = butcherReportReadService.findById(58l).getResult();

 //       return animalCertificateService.isSucceed(breedReport,breedReportSurveyRecord).getResult();

        return creditProductService.isSucceed(butcherReport,butcherReportSurveyRecord).getResult();

    }

    @RpcConsumer
    private AnimalMarkReadService animalMarkReadService;


    @RpcConsumer
    private CasesBasicService casesBasicService;
    @RpcConsumer
    private CauseActionsService causeActionsService;

    @RpcConsumer
    private SimplenessCasesService simplenessCasesService;


    @RpcConsumer
    private WritDetailsService writDetailsService;

    @RpcConsumer
    private WritTypeService writTypeService;
    @GetMapping(value = "/test/AB1")
    public  Map<Integer, String> testAB1(){
        Response<Map<Integer,String>> response = writTypeService.allType();
        if (!response.isSuccess()){
            log.error("failed to find allType, cause : {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @RpcConsumer
    private PartiesInformationService partiesInformationService;
    @PostMapping(value = "/test/AB2")
    public  Long testAB2(PartiesInformation partiesInformation){
        partiesInformation.setAge(20);
        partiesInformation.setDelete_flag(1);
        partiesInformation.setGender(1);
        Response<Long> response = partiesInformationService.create(partiesInformation);
        if(!response.isSuccess()){
            log.error("failed to create partiesInformation read by partiesInformation = {}, cause : {}", partiesInformation, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();


    }



    @RpcConsumer
    private AppUserRoleReadService appUserRoleReadService;
    @RpcConsumer
    private UserReadService<User> userReadService;

    @GetMapping(value = "/test/AB3")
    public Map<Integer, String> testAB3(Integer type){
        Response<Map<Integer,String>> response = writTypeService.allDetailedTypeByType(type);
        if (!response.isSuccess()){
            log.error("failed to find allDetailedType, cause : {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    @Autowired
    private SendMessage sendMessage;

    @RpcConsumer
    private SceneCollectService sceneCollectService;
    @GetMapping(value = "/test/sendmsg")
    private Long SendMobile() {
        SceneCollect sceneCollect = sceneCollectService.finById(1l).getResult();

        Response<Long> response = sceneCollectService.create(sceneCollect);
        if(!response.isSuccess()){
            log.error("failed to create sceneCollect read by sceneCollect = {}, cause : {}", sceneCollect, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @GetMapping(value = "/test/sendmsgaa")
    private Long SendMobileaa() {
        SceneCollect sceneCollect = sceneCollectService.finById(1l).getResult();
        Map<String , Map<String , String>> mapMap = sceneCollect.getPhotoMap();
        Map<String , String> map2 = mapMap.get("url1");
        map2.put("url22","12345");
        mapMap.put("url2",map2);
//        Map<String , Map<String , String>> map = new HashMap<>();

        sceneCollect.setPhotoMap(mapMap);
        Response<Long> response = sceneCollectService.create(sceneCollect);
        return response.getResult();


    }

}
